<template>
	<view class="bg-white">
		<view class="text-center text-xl ">问卷内容</view>
		<!-- index ：数组下标-->
		<view class="text-lg" v-for="(item,index) in questionnaires" :key="index">
			<view class="">{{index+1}}.{{item.question}}</view>
			<radio-group name="" @change="chioseChange" :data-quest-index="index">
				<label v-for="(i, index2) in item.answer" :key="index2">
					<view :data-score="i.socre">
					<!-- 选择圈 圆的大小 圆的样式 ：按比例缩放  value="i.socre：一道选项它的分值-->
						<radio style="transform:scale(0.7)" :value="i.score" /><text>{{i.content}}</text>
					</view>
				</label>
			</radio-group>
		</view>


		<!-- type:按钮样式类型 -->

		<view class="padding flex flex-direction">
			<button class="cu-btn bg-blue lg" @click="submit">提交</button>
		</view>


	</view>
</template>

<script>
	import questions from '@/common/data/question.list.js'
	export default {
		data() {
			return {
				questionnaires: [],
				result: []
			}
		},
		onLoad() {
			let index = Math.floor(Math.random() * 2);
			let questionPaper = questions.question[index];
			this.questionnaires = questionPaper;
			// 42-50行：卷子的成绩初始化
			// questionPaper.length：卷子题目长度
			let temparr = [];
			for (var i = 0; i < questionPaper.length; i++) {
				let answer = {
					chiosed: false,
					score: 0
				}
				temparr.push(answer);
			}
			this.result = temparr;
		},
		methods: {
			submit() {
				let isOk = true;
				this.result.forEach(i => {
					//===:强等 ==：近似值 =赋值
					if (i.chiosed === false) {
						isOk = false
					}
				})
				if (!isOk) {
					uni.showModal({
						title: '提交失败',
						content: '问卷填写未完成',
						showCancel: false
					})
					return;
				}
				let countsum = 0
				this.result.forEach(i => {
					countsum += Number(i.score)
				})

				for (let index = 0; index < questionnaires.length; index++) {
					console.log(index + 1);
					var item = questionnaires[index]
				}
				
				uni.showModal({
					title: '得分 ' + countsum,
					content: '总分数大于2分存在风险,小于2分为低风险',
					showCancel: false
				})
			},
			// chioseChange:选择变化
			chioseChange(e) {
				// e.target.dataset.questIndex：对象属性，一层一层的往下走
				let i = e.target.dataset.questIndex;
				console.log(e);
				let score = e.detail.value;
				this.result[i].chiosed = true;
				this.result[i].score = score;
			}
		}
	}
</script>

<style>
	page {
		background-color: #FFFFFF;
	}
</style>
